package project.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import project.entity.TJdlx;
import project.service.TJdlxService;
import project.util.ResponseUtil;
import project.util.UUIDUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 景点类型表 前端控制器
 * </p>
 *
 * @author YJR
 * @since 2023-07-25
 */
@RestController
@RequestMapping("/api/jdlx/v1")
@Slf4j
public class TJdlxController {
    @Resource
    private TJdlxService jdlxService;

    /**
     * 添加或者更新景点分类
     * @param jdlx
     * @return
     */
    @RequestMapping(value="/addOrUpdate",method = RequestMethod.POST)
    public JSONObject addOrUpdate(@RequestBody TJdlx jdlx) {
        try {
            if (StringUtils.isBlank(jdlx.getcId())) {
                jdlx.setcId(UUIDUtil.getUUID());
            }
            return ResponseUtil.getSuccessResponseBody( jdlxService.saveOrUpdate(jdlx));
        } catch (Exception e) {
            log.error("addOrUpdate出现异常", e);
            return ResponseUtil.getErrorResponseBody(null, "服务出现异常");
        }
    }

    /**
     * 分页查询
     * @param param
     * @return
     */
    @RequestMapping(value="/select",method = RequestMethod.POST)
    public JSONObject selectSp(@RequestBody JSONObject param){
        // 获取页数
        Integer current = param.getInteger("current");
        // 分类名称
        String name = param.getString("flmc");

        Page<TJdlx> page = new Page<TJdlx>();
        // 设置每页数量
        page.setSize(10);

        if(current != null){
            page.setCurrent(current);
        }else{
            page.setCurrent(1);
        }

        QueryWrapper<TJdlx> wrapper = new QueryWrapper<>();
        // 未删除的
        wrapper.lambda().eq(TJdlx::getnDel,0);
        // 分类名称
        if(StringUtils.isNotBlank(name)){
            wrapper.lambda().like(TJdlx::getcFl,name);
        }

        // 获取查询返回结果
        Page<TJdlx> pageSelect = jdlxService.page(page, wrapper);
        JSONObject object = new JSONObject();
        object.put("list",pageSelect.getRecords());
        object.put("total",pageSelect.getTotal());
        return ResponseUtil.getSuccessResponseBody(object);
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public boolean del(@RequestBody TJdlx tJdlx){
        tJdlx.setnDel(1);
        return jdlxService.saveOrUpdate(tJdlx);
    }

    @RequestMapping(value = "/wx/getJdlx",method = RequestMethod.POST)
    public List<TJdlx> getJdlx(@RequestBody JSONObject param){
        QueryWrapper<TJdlx> queryWrapper = new QueryWrapper<>();
        // 未被删除
        queryWrapper.lambda().eq(TJdlx::getnDel,0);
        return jdlxService.list(queryWrapper);
    }


}
